#!/bin/sh
#(c) Robert Shingledecker 2009-2011
#added code for exquisite jlslegalize@gmail.com

. /etc/init.d/tc-functions
#jls start
. /etc/init.d/e-tc-functions
#jls end

alias awk="busybox awk"
alias cp="busybox cp"
alias cpio="busybox cpio"
alias depmod="busybox depmod"
alias grep="busybox grep"
alias ls="busybox ls"
alias mount="busybox mount"
alias umount="busybox umount"

checkroot

read USER < /etc/sysconfig/tcuser
TCEINSTALLED="/usr/local/tce.installed"
#jls start
EXQUISITE="$(pidof exquisite)"
WRITE="/usr/local/bin/exquisite-write"
PROGRESS=0
#jls end

process_normal_tcedir() {
	[ -z "$TCE" ] && TCE="$(autoscan 'tce' 'd')"/tce
	if [ "$TCE" != "/tce" ]; then
		TCE_DEVICE="${TCE%%/*}"
		TCE_DIR="${TCE#*/}"
		if [ "$TCE_DEVICE" = "$TCE_DIR" ]; then TCE_DIR="tce"; fi
		find_mountpoint "$TCE_DEVICE"
		if [ -n "$MOUNTPOINT" ]; then
			if [ "$MOUNTED" == "yes" ]; then
				setupExtnDirs
			else
				mount "$MOUNTPOINT"
				if [ $? == 0 ]; then
					UMOUNT="yes"
					setupExtnDirs
				fi
			fi
		fi
	else
	   MOUNTPOINT="/tmp"; TCE_DIR="tce"
	fi

	TCEDIR="$MOUNTPOINT"/"$TCE_DIR"
	if [ ! -d "$TCEDIR"/optional ]; then
		mkdir "$TCEDIR"/optional
		chown -R "$USER".staff "$TCEDIR"
	fi
	[ -L /etc/sysconfig/tcedir ] && rm -f /etc/sysconfig/tcedir
	ln -sf "$TCEDIR" /etc/sysconfig/tcedir
	chown -R "$USER".staff "$TCEINSTALLED"
	sync
	[ "$MOUNTPOINT" == "/tmp" ] && return

# 	Process any .gzs in tce directory before regular extensions.
	if [ "$(ls -A "$TCEDIR"/*.gz 2>/dev/null)" ]; then
		process_gz "$TCEDIR"
	fi

	if [ -d "$TCEDIR"/optional/upgrade ] && [ "$(ls -A "$TCEDIR"/optional/upgrade 2>/dev/null)" ]; then
		echo -n "${GREEN}Performing upgrade from $TCEDIR/optional/upgrade. ${NORMAL}"
		#jls start
		$WRITE "TEXT Performing upgrade from $TCEDIR/optional/upgrade."
		#jls end
		yes y | cp -ai "$TCEDIR"/optional/upgrade/* "$TCEDIR"/optional/. 2>/dev/null && rm -rf "$TCEDIR"/optional/upgrade
		echo "${YELLOW}Done.${NORMAL}"
		$WRITE "SUCCESS OK"
		[ "$BOOTING" == "booting" ] && $WRITE "MSG  " #jls
	fi
#	Process regular extensions	
	LOADFROM="$TCEDIR"/optional
	ONBOOT="$TCEDIR"/"$TARGETLIST"
	if [ -s "$ONBOOT" ];then
		[ "$BOOTING" == "booting" ] && $WRITE "MSG Loading tcz extensions" && percent_calc $ONBOOT
		for FILE in `cat "$ONBOOT"`; do FILE="$LOADFROM/$FILE" && process; done
		[ "$BOOTING" == "booting" ] && $WRITE "TEXT tcz extensions" && $WRITE "SUCCESS OK"
	fi
	[ "$BOOTING" == "booting" ] && $WRITE "MSG  " && $WRITE "PULSATE"
	sync
}

setupExtnDirs() {
	[ -d "$MOUNTPOINT"/"$TCE_DIR"/optional ] || mkdir -p "$MOUNTPOINT"/"$TCE_DIR"/optional
	[ -d "$MOUNTPOINT"/"$TCE_DIR"/ondemand ] || mkdir -p "$MOUNTPOINT"/"$TCE_DIR"/ondemand
	touch "$MOUNTPOINT"/"$TCE_DIR"/onboot.lst
	chown -R "$USER".staff "$MOUNTPOINT"/"$TCE_DIR" 2>/dev/null
	chmod -R g+w "$MOUNTPOINT"/"$TCE_DIR" 2>/dev/null
}

process_gz() {
	FILELIST=`ls "$1"/*.gz`
	#jls start
	if [ -s $FILELIST ]; then
		percent_calc $FILELIST
	fi
	#jls end
	cd /
	for FILE in $FILELIST; do
		zcat "$FILE" | cpio -idmu > /dev/null 2>&1
	done
	cd -
	setupHome
}

process_scms() {
	if [ -s "$SCMLIST" ]; then
#		echo "SCMLIST $SCMLIST"
		[ "$BOOTING" == "booting" ] && $WRITE "MSG Loading scm extensions" && percent_calc $SCMLIST
#		echo "PERCENT $PERCENT"
		while read S
		do
			[ "$BOOTING" == "booting" ] && $WRITE "TEXT Loading $FILE"
			su "$USER" -c 'scm-load -ib '"$LOADFROM"/"$S".scm
			[ "$BOOTING" == "booting" ] && PROGRESS=$(($PROGRESS + $PERCENT )) && $WRITE "PROGRESS $PROGRESS" && $WRITE "SUCCESS OK" 
		done < "$SCMLIST"
		[ "$BOOTING" == "booting" ] && $WRITE "TEXT scm extensions" && $WRITE "SUCCESS OK" && $WRITE "MSG  "
	fi
}

process() {
	BASENAME=`basename "$FILE"`
	APPNAME="${BASENAME%.tcz}"
	[ -f "$TCEINSTALLED"/"$APPNAME" ] && return
	[ "$BOOTING" == "booting" ] && $WRITE "TEXT Loading $FILE"
	FLAGS=" -i -b "
	su "$USER" -c 'tce-load '"$FLAGS"' '"$FILE"
	if [ -s /tmp/aberr ]; then
		echo "occured while loading: " "$FILE" >> /tmp/aberr
		[ "$BOOTING" == "booting" ] && $WRITE "TEXT occured while loading: $FILE" && $WRITE "FAILURE FAIL"
		exit 1
	fi
	[ "$BOOTING" == "booting" ] && PROGRESS=$(($PROGRESS + $PERCENT )) && $WRITE "PROGRESS $PROGRESS" && $WRITE "SUCCESS OK" 
}

process_CD() {
	CD="/mnt/${DEV#/dev/}"
	mount "$CD" 2>/dev/null
	if [ "$?" == 0 ] ; then
		if [ -d "$CD"/tce ]; then
			LOADFROM="$CD"/tce/optional
			CDELIST="$CD"/tce/"$CDELST"
			if [ -s "$CDELIST" ]; then
				[ "$BOOTING" == "booting" ] && $WRITE "MSG Loading (pseudo) CD extensions"
				percent_calc $CDELIST
				while read FILE; do
					FILE="$LOADFROM/$FILE"
					process
				done < "$CDELIST"
				echo "$LOADFROM" > /etc/sysconfig/cde
			fi
			SCMLIST="$CD"/tce/scmboot.lst
			process_scms
			[ "$BOOTING" == "booting" ] && $WRITE "TEXT (pseudo) CD extensions" && $WRITE "SUCCESS OK" && $WRITE "MSG  "
		else
			umount "$CD"
		fi
	fi
}
BOOTING=$1
for i in `cat /proc/cmdline`; do
	case $i in
		tce=*) TCE=${i#*=} ;;
		lst=*) TARGETLIST=${i#*=} ;;
	esac
	case $i in
		cde) CDE=1 ;;
		base) ONLYBASE=1 ;;
	esac
done

[ -n "$TARGETLIST" ] || TARGETLIST="onboot.lst"
[ -n "$CDELST" ] || CDELST=$TARGETLIST #jls

# Process any .gzs before attempting to mount tce directory.
# /tmp/builtin support provided for embedding extensions inside the initrd.
if [ "$(ls -A "/tmp/builtin"/*.gz 2>/dev/null)" ]; then
	process_gz "/tmp/builtin"
fi

# Next Check for any tcz initrd builtins
if [ -d /tmp/builtin ] && [ "$(ls -A /tmp/builtin 2>/dev/null)" ]; then
	TCEDIR=/tmp/builtin
	LOADFROM="$TCEDIR"/optional
	if [ -s "$TCEDIR"/"$TARGETLIST" ];then
		[ "$BOOTING" == "booting" ] && $WRITE "MSG Loading tcz initrd builtins extensions" && percent_calc $TARGETLIST
	       	for FILE in `cat "$TCEDIR"/"$TARGETLIST"`; do FILE="$LOADFROM/$FILE" && process; done
		[ "$BOOTING" == "booting" ] && $WRITE "TEXT tcz initrd builtins extensions" && $WRITE "SUCCESS OK" && $WRITE "MSG  "
       fi
	SCMLIST="$TCEDIR"/scmboot.lst
	process_scms
fi

# Next process regular extensions unless BASE boot option has been specified.

if [ "$BOOTING" == "booting" ] && [ -n "$ONLYBASE" ]; then
	echo "${GREEN}Skipping regular Extension loading as requested on the command line.${NORMAL}"
	exquisite-write "TEXT Skipping regular Extension loading as requested on the command line."
	TCEDIR="/tmp/tce"
	if [ ! -d "$TCEDIR"/optional ]; then
		mkdir "$TCEDIR"/optional
		chown -R "$USER".staff "$TCEDIR"
	fi
else
	process_normal_tcedir
fi
[ "$BOOTING" == "booting" ] && [ -z "$EXQUISITE" ] && exquisite -fb -fs -t /usr/local/share/exquisite/data/themes/eCore.edj -cleanvt -verbose & #jls

# Finally check for CD Extensions if requested
if [ "$CDE" -a -s /etc/sysconfig/cdroms ]; then
	for DEV in `cat /etc/sysconfig/cdroms`; do
		process_CD
	done 
fi			
[ "$BOOTING" == "booting" ] && [ -z "$EXQUISITE" ] && exquisite -fb -fs -t /usr/local/share/exquisite/data/themes/eCore.edj -cleanvt -verbose & #jls

# If nothing loaded then also check for pseudo CD, e.g., isohybrid
if [ "$CDE" -a -z "$CDELIST" ]; then
	sleep 5
	DEV="$(autoscan 'tce' 'd')"
	process_CD
fi
[ "$BOOTING" == "booting" ] && [ -z "$EXQUISITE" ] && exquisite -fb -fs -t /usr/local/share/exquisite/data/themes/eCore.edj -cleanvt -verbose & #jls

# Wrap up by performing updates as may be needed.
if [ -e /etc/sysconfig/newmodules ]; then
	depmod -a 2>/dev/null
	/sbin/udevadm trigger
fi
/sbin/ldconfig 2>/dev/null
if [ -s /tmp/setup.lst ]; then
	for F in `cat /tmp/setup.lst`; do "$F"; done
	rm -f /tmp/setup.lst
fi

SCMLIST="$TCEDIR"/scmboot.lst
LOADFROM="$TCEDIR"/optional
process_scms

setupHome
